package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;


public class Application extends Controller {

    public static void index() {
	long time = System.currentTimeMillis();
		renderArgs.put("connected", ""+time);
	
        render();

		
    }


	//============== GAME MECHANICS
       private static Developer getApp(String appKey){
				
			return Developer.find("byAppKey",appKey).first();
			
			
	   }

	//============== POINTS
		public static void getAllPoints(){
		    			
			List<Point> p = Point.find("byApplication",getApp(params.get("appKey"))).fetch();
			renderJSON(p);
		}
		
		public static void getPoints(String uniqueId){
			Point p = Point.find("byUniqueIdAndApplication", uniqueId, getApp(params.get("appKey"))).first();
			renderJSON(p);
			
		}
		
		public static void addPoints(String uniqueId, int value){
			Point p = Point.find("byUniqueIdAndApplication", uniqueId, getApp(params.get("appKey"))).first();
			p.defaultValue += value;
			p.save();
			AppReward  rewardCondition = ConditionManager.processAllConditions(params.get("appKey"));
			
			//	rewardCondition
			for(String key : rewardCondition.getPoints().keySet()){
				System.out.println("key ="+key);
				rewardCondition.setPointValue(key, value);
			}
			System.out.println("point add ="+value);
			
			renderJSON(rewardCondition);
			
		}

		public static void deletePoints(){

		}

	//============== LEVELS
		public static void getAllLevels(){
			List<Level> p = Level.find("byApplicationId",getApp(params.get("appKey"))).fetch();
			renderJSON(p);
		}

		public static void getLevel(String uniqueId){
			Level l = Level.find("byUniqueIdAndApplicationId", uniqueId, getApp(params.get("appKey"))).first();
			renderJSON(l);
		}
		
		public static void getCurrentLevel(){
			List<Level> l = Level.find("byCurrentAndApplicationId", true, getApp(params.get("appKey"))).fetch();
			renderJSON(l);
		}

	//============== LEADERBOARDS

		public static void getAllLeaderboards(){
			List<Leaderboard> p = Leaderboard.all().fetch();
			renderJSON(p);
		}

		public static void getLeaderboards(String uniqueId){
			Leaderboard p = Leaderboard.find("byUniqueId", uniqueId).first();
			renderJSON(p);
		}

	//============== CHALLENGERS

		public static void getAllChallengers(String completed){
			boolean complete = false;
			if(completed.equals("true")){
				complete = true;
			}
			List<Challenger> p = Challenger.find("byApplicationIdAndCompleted",getApp(params.get("appKey")), complete).fetch();
			renderJSON(p);
		}
		
		

		public static void getChallengers(String uniqueId){
			Challenger p = Challenger.find("byUniqueIdAndApplicationId", uniqueId, getApp(params.get("appKey"))).first();
			renderJSON(p);
		}

		public static void completeChallenger(String uniqueId){
			AppReward  reward = new AppReward();
			Challenger c = Challenger.find("byUniqueIdAndApplicationId", uniqueId, getApp(params.get("appKey"))).first();
			c.completed = true;
			
			for(Reward r : c.rewards) {
				r.execute();
				r.save();
				
				reward.badges.add(r.badgeType);
				AppPoint p = new AppPoint();
				 
				 p.type = r.pointType.title;
				 p.addValue(r.pointValue);
				 reward.addPoint(r.pointType.title, p);
			}
			c.save();
			AppReward  rewardCondition = ConditionManager.processAllConditions(params.get("appKey"));
			AppPoint p = new AppPoint();
			
			for(String key : rewardCondition.getPoints().keySet()){
				
				reward.addPoint(key, rewardCondition.getPoints().get(key));
			}	
			
			
			for(Badge b : rewardCondition.badges) {	
				reward.badges.add(b);
			}	
			renderJSON(reward);
		}
		
	//============== BADGES
	
		public static void getAllBadges(){
			List<Badge> p = Badge.find("byApplicationId",getApp(params.get("appKey"))).fetch();
			renderJSON(p);
		}
		
		public static void getAllBadgesByParam(String recived){
			boolean complete = false;
			if(recived.equals("true")){
				complete = true;
			}
			List<Badge> p = Badge.find("byApplicationIdAndRecived",getApp(params.get("appKey")),complete).fetch();
			renderJSON(p);
		}
		
		public static void getBadge(String uniqueId){
			Badge p = Badge.find("byUniqueIdAndApplicationId", uniqueId, getApp(params.get("appKey"))).first();
			renderJSON(p);
		}

	//============== 
	
	

}